package com.lemon.smartsafetynew.ui.adapter.electrical;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.electrical.ElectricalKeyVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2018/3/12.
 */

public class ElectricialListItemAdapter extends LemonRcvAdapter<ElectricalKeyVo> {

    private RecyclerTClickListener<ElectricalKeyVo> mListener;

    public ElectricialListItemAdapter(@Nullable List<ElectricalKeyVo> data,
                                      RecyclerTClickListener<ElectricalKeyVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new ElectricialListItem();
    }

    public class ElectricialListItem implements AdapterItem<ElectricalKeyVo> {


        private ElectricalKeyVo data;
        private int pos;

        @Bind(R.id.item_divided)
        View itemDivided;
        @Bind(R.id.monitor_item_name)
        TextView monitorItemName;
        @Bind(R.id.monitor_item_value)
        TextView monitorItemValue;
        @Bind(R.id.monitor_item_status_imageView)
        ImageView monitorItemStatusImageView;
        @Bind(R.id.item_detail_layout)
        LinearLayout itemDetailLayout;
        @Bind(R.id.monitor_item_status)
        TextView monitorItemStatus;

        @Override
        public int getLayoutResId() {
            return R.layout.item_monitor_detail;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {
            monitorItemStatusImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(data, pos);
                }
            });
        }

        @Override
        public void handleData(ElectricalKeyVo vo, int position) {

            data = vo;
            pos = position;
            monitorItemName.setText(CommonUtil.avoidNullString(vo.getKey()));
            if (position == 0) {
                itemDivided.setVisibility(View.VISIBLE);
            } else {
                itemDivided.setVisibility(View.GONE);
            }
            if(TextUtils.isEmpty(vo.getStatus())){
                monitorItemValue.setTextColor(Color.parseColor("#D3D2D0"));  //无数据
                monitorItemStatus.setTextColor(Color.parseColor("#D3D2D0"));
                monitorItemStatus.setText("无数据");
            }else if (vo.getStatus().equals("0")) {
                monitorItemValue.setTextColor(Color.parseColor("#EA654F"));  //异常
                monitorItemStatus.setTextColor(Color.parseColor("#EA654F"));
                monitorItemStatus.setText("异常");
            } else {
                monitorItemValue.setTextColor(Color.parseColor("#60CC86"));  //正常
                monitorItemStatus.setTextColor(Color.parseColor("#60CC86"));
                monitorItemStatus.setText("正常");
            }
            if(TextUtils.isEmpty(vo.getValue())){
                monitorItemValue.setText("--");
            }else {
                monitorItemValue.setText(vo.getValue());
            }
        }
    }
}
